package com.example.demo.service.exam.impl;

import com.example.demo.base.config.auth.UserInfoShareHolder;
import com.example.demo.domain.CombineClause;
import com.example.demo.domain.SingleClause;
import com.example.demo.domain.entity.User;
import com.example.demo.domain.exam.Exam;
import com.example.demo.domain.exam.Sorting;
import com.example.demo.domain.exam.ExamSubmitLog;
import com.example.demo.dto.LatestSubmissionResultDto;
import com.example.demo.exception.ExceptionUtils;
import com.example.demo.mapper.ExamSubmitLogMapper;
import com.example.demo.service.exam.ExamSubmitLogService;
import com.example.demo.service.impl.BaseServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
public class ExamSubmitLogServiceImpl extends BaseServiceImpl<ExamSubmitLogMapper, ExamSubmitLog> implements ExamSubmitLogService {

    private final ExamSubmitLogMapper submitLogMapper;

    @Override
    public void insert(ExamSubmitLog entity) {
        User user = UserInfoShareHolder.getUserInfo().getUser();
        Long userId = user.getId();
        String username = user.getUsername();
        this.valid(entity);
        entity.setSubmitTime(new Timestamp(System.currentTimeMillis()));
        entity.setUserId(userId);
        entity.setUsername(username);
        submitLogMapper.insert(entity);
    }

    @Override
    public List<ExamSubmitLog> getSubmitLogByExamId(Long examId) {   //
        Optional.ofNullable(examId).orElseThrow(() -> ExceptionUtils.exception("考试标识不能为空"));
        return super.selectList(SingleClause.equal("exam_id", examId), Sorting.ascending("user_id"), Sorting.ascending("submit_time"));
    }

    @Override
    public ExamSubmitLog getSubmitLogBySubmissionId(Long submissionId) {
        if (submissionId == null) {
            throw ExceptionUtils.exception("提交评测记录标识不能为空");
        }
        ExamSubmitLog submitLog = super.selectOne(SingleClause.equal("submission_id", submissionId));
        System.out.println("EXAM 查询结果:  "+ submitLog);
        return submitLog;
    }



    @Override
    public void deleteByExamUserId(Long examId, Long userId) {
        Optional.ofNullable(examId).orElseThrow(() -> ExceptionUtils.exception("考试标识不能为空"));
        Optional.ofNullable(userId).orElseThrow(() -> ExceptionUtils.exception("用户标识不能为空"));
        super.delete(CombineClause.and(
                SingleClause.equal("exam_id", examId),
                SingleClause.equal("user_id", userId)
        ));
    }

    @Override
    public void update(ExamSubmitLog entity) {
        Optional.ofNullable(entity.getId()).orElseThrow(() -> ExceptionUtils.exception("更新时主键标识不能为空"));
        super.updateById(entity);
    }

    @Override
    public List<LatestSubmissionResultDto> getQuestionSubmitLog(Long questionID, Long examId) {
        return null;
    }

    private void valid(ExamSubmitLog entity) {
        Long examId = entity.getExamId();
        Long paperId = entity.getPaperId();
        Long questionId = entity.getQuestionId();
        Timestamp startTime = entity.getStartTime();
        if (examId == null) {
            throw ExceptionUtils.exception("考试标识不能为空");
        }
        if (paperId == null) {
            throw ExceptionUtils.exception("试卷标识不能为空");
        }
        if (questionId == null) {
            throw ExceptionUtils.exception("试题标识不能为空");
        }
        if (startTime == null) {
            throw ExceptionUtils.exception("考试时间不能为空");
        }
    }

}
